855B - Marvolo Gaunt's Ring - CodeForces Solution


brute force data structures dp *1500

Please click on ads to support us..

Python Code:

n,p,q,r = [int(x) for x in input().split()]
a= [int(x) for x in input().split()]
m = len(a)
max1= [None]*m
pmax = [None]*m
rmax= [None]*m
j=m-1
pmax[0] = p*a[0]
rmax[m-1] = r*a[m-1]
for i in range(1,m):
    pmax[i] = max(pmax[i-1], (p*a[i]))
    rmax[j-1] = max(rmax[j], (r*a[j-1]))
    j-=1
max1[0] = pmax[0]+q*a[0]+rmax[0]    
for k in range(1,m):
    max1[k] = max(max1[k-1], pmax[k]+q*a[k]+rmax[k])
print(max1[m-1])

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define Ain(A, n)              \
    for (ll i = 0; i < n; i++) \
    {                          \
        cin >> A[i];           \
    }
#define MAX(A) *max_element(A.begin(), A.end())
#define MIN(A) *min_element(A.begin(), A.end())

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n, p, q, r;
    cin >> n >> p >> q >> r;

    ll valP = INT64_MIN;
    ll valQ = INT64_MIN;
    ll valR = INT64_MIN;

    ll a;

    for (int i = 1; i <= n; i++)
    {
        cin >> a;
        valP = max(valP, p * a);
        valQ = max(valQ, valP + q * a);
        valR = max(valR, valQ + r * a);
    }
    cout << valR << endl;

    return 0;
}


Comments

Submit
0 Comments
More Questions

1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array